package jjn.round1;

import commons.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author Jjn
 * @since 2022/9/18 08:20
 */
public class Offer_32_PrintBinaryTree {
    public int[] levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        List<Integer> list = new ArrayList<>();
        while (!queue.isEmpty()) {
            TreeNode top = queue.poll();
            if (top != null) {
                list.add(top.val);
                TreeNode left = top.left;
                if (left != null) {
                    queue.offer(left);
                }
                TreeNode right = top.right;
                if (right != null) {
                    queue.offer(right);
                }
            }
        }
        int[] result = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = list.get(i);
        }
        return result;
    }
}
